<template>
  <div></div>
</template>
<script>
import { getList } from "network/mock.js";

// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入柱状图图表，图表后缀都为 Chart
import { BarChart, LineChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系组件，组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
} from "echarts/components";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
  BarChart,
  LineChart,
  CanvasRenderer,
]);
export default {
  props: ["bar", "year"],
  data() {
    return {
      chart: null,
      barC: null,
      year1: null,
      year2: null,
      isYear: 2020,
    };
  },
  watch: {
    bar: function(newValue) {
      this.barC = newValue;
      newValue &&
        this.getBarChart(() => {
          this.init();
        });
    },
    year(newVal) {
      this.isYear = newVal;
      // 监听年份的变化 如果有变 再重新渲染一次
      newVal && this.init();
    },
  },
  mounted() {},
  methods: {
    // 请求数据
    async getBarChart(callback) {
      let { list } = await getList();
      this.year1 = list.find((r) => r.type === "LeftMidEchart").res1;
      this.year2 = list.find((r) => r.type === "LeftMidEchart").res2;
      callback && callback();
    },
    // 初始化
    init() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.barC);
      // 声明颜色数组
      myChart.setOption({
        color: ["#00f2f1 ", " #ed3f35"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross", //shadow  line
          },
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "4%",
          bottom: "3%",
          show: true, // 显示边框
          borderColor: "#012f4a", // 边框颜色
          containLabel: true, // 包含刻度文字在内
        },
        legend: {
          // 如果series有name值了 可以不用谢data
          // data: ["邮件营销", "联盟广告"],
          textStyle: {
            color: "#4c9bfd",
            fontSize: 12,
          },
          right: "10%", //距离右边百分之十
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: [
            "1月",
            "2月",
            "3月",
            "4月",
            "5月",
            "6月",
            "7月",
            "8月",
            "9月",
            "10月",
            "11月",
            "12月",
          ],
          axisLine: {
            show: false,
          },
          // 不显示刻度
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#4c9bfd", // 文本颜色
          },
        },
        yAxis: {
          type: "value",
          axisLine: {
            show: false,
          },
          // 不显示刻度
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#4c9bfd", // 文字颜色
          },
          // 修改刻度线的颜色
          splitLine: {
            lineStyle: {
              color: "#012f4a",
            },
          },
        },
        series: [
          {
            name: "新增就业",
            type: "line",
            smooth: true,
            data: this.isYear == 2020 ? this.year1.data1 : this.year2.data1,
          },
          {
            name: "新增岗位",
            type: "line",
            smooth: true,
            data: this.isYear == 2020 ? this.year1.data2 : this.year2.data2,
          },
        ],
      });
      // 设置图表的自动适应
      window.addEventListener("resize", function() {
        myChart.resize();
      });
    },
  },
};
</script>
<style scoped lang="scss"></style>
